*Create results*

clear

use "Results\tab_B3\Scenario_5\dataset_with_singles_3_and_5_types.dta"

gen ambition=ambition_type_k_5_s

//generate partners' ambition
by couple_id aar, sort: gen am_male = ambition if koen=="1" & relationship==1
by couple_id aar: gen am_female = ambition if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(am_male)
by couple_id aar: replace am_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(am_female)
by couple_id aar: replace am_female = maxeduc
drop maxeduc


*Ambition
*first parts of 1980
forvalues i=1980(1)1980{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]+mat_`i'_2[1,5]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]+mat_`i'_2[2,5]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]+mat_`i'_2[3,5]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]+mat_`i'_2[4,5]
sca male_5_`i'=mat_`i'_2[5,1]+mat_`i'_2[5,2]+mat_`i'_2[5,3]+mat_`i'_2[5,4]+mat_`i'_2[5,5]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]+mat_`i'_2[5,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]+mat_`i'_2[5,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]+mat_`i'_2[5,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]+mat_`i'_2[5,4]
sca female_5_`i'=mat_`i'_2[1,5]+mat_`i'_2[2,5]+mat_`i'_2[3,5]+mat_`i'_2[4,5]+mat_`i'_2[5,5]

*unweighted likelihood indices
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

}

*then rest of loop
foreach i in 1981 2018{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]+mat_`i'_2[1,5]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]+mat_`i'_2[2,5]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]+mat_`i'_2[3,5]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]+mat_`i'_2[4,5]
sca male_5_`i'=mat_`i'_2[5,1]+mat_`i'_2[5,2]+mat_`i'_2[5,3]+mat_`i'_2[5,4]+mat_`i'_2[5,5]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]+mat_`i'_2[5,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]+mat_`i'_2[5,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]+mat_`i'_2[5,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]+mat_`i'_2[5,4]
sca female_5_`i'=mat_`i'_2[1,5]+mat_`i'_2[2,5]+mat_`i'_2[3,5]+mat_`i'_2[4,5]+mat_`i'_2[5,5]

*unweighted likelihood indices
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

*optimal lambda cf. paper with Bastian
sca xi1_`i'=((mat_`i'_2[1,1]/((1-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[5,5]/((male_5_`i')^2)))*(male_5_`i'-male_5_1980)+((mat_`i'_2[1,1]/((1-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[4,4]/((male_4_`i')^2)))*(male_4_`i'-male_4_1980)+((mat_`i'_2[1,1]/((1-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[3,3]/((male_3_`i')^2)))*(male_3_`i'-male_3_1980)+((mat_`i'_2[1,1]/((1-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[2,2]/((male_2_`i')^2)))*(male_2_`i'-male_2_1980)

sca xi2_`i'=((mat_`i'_2[1,1]/((1-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[5,5]/((female_5_`i')^2)))*(female_5_`i'-female_5_1980)+((mat_`i'_2[1,1]/((1-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[4,4]/((female_4_`i')^2)))*(female_4_`i'-female_4_1980)+((mat_`i'_2[1,1]/((1-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[3,3]/((female_3_`i')^2)))*(female_3_`i'-female_3_1980)+((mat_`i'_2[1,1]/((1-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[2,2]/((female_2_`i')^2)))*(female_2_`i'-female_2_1980)

gen temp=.
replace temp=0 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')<abs(xi2_`i')
replace temp=(xi1_`i'/(xi1_`i'-xi2_`i')) if sign(xi1_`i')!=sign(xi2_`i')
replace temp=1 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')>abs(xi2_`i')

sca lambda_`i'=temp
drop temp

*weights
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca weight_`m'_`f'_`i'=lambda_`i'*male_`m'_`i'+(1-lambda_`i')*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'+S_5_5_`i'

}

*finish 1980 part

forvalues i=1980(1)1980{
	
*weights
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca weight_`m'_`f'_`i'=lambda_1981*male_`m'_`i'+(1-lambda_1981)*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)5{
	forvalues f=1(1)5{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'+S_5_5_`i'

}

**Add population marignals incl. singles and diagonal pi's

foreach i in 1980 1981 2018{
*marginals
sca male_1_`i'_marr=mat_`i'[1,1]+mat_`i'[1,2]+mat_`i'[1,3]+mat_`i'[1,4]+mat_`i'[1,5]
sca male_2_`i'_marr=mat_`i'[2,1]+mat_`i'[2,2]+mat_`i'[2,3]+mat_`i'[2,4]+mat_`i'[2,5]
sca male_3_`i'_marr=mat_`i'[3,1]+mat_`i'[3,2]+mat_`i'[3,3]+mat_`i'[3,4]+mat_`i'[3,5]
sca male_4_`i'_marr=mat_`i'[4,1]+mat_`i'[4,2]+mat_`i'[4,3]+mat_`i'[4,4]+mat_`i'[4,5]
sca male_5_`i'_marr=mat_`i'[5,1]+mat_`i'[5,2]+mat_`i'[5,3]+mat_`i'[5,4]+mat_`i'[5,5]


sca female_1_`i'_marr=mat_`i'[1,1]+mat_`i'[2,1]+mat_`i'[3,1]+mat_`i'[4,1]+mat_`i'[5,1]
sca female_2_`i'_marr=mat_`i'[1,2]+mat_`i'[2,2]+mat_`i'[3,2]+mat_`i'[4,2]+mat_`i'[5,2]
sca female_3_`i'_marr=mat_`i'[1,3]+mat_`i'[2,3]+mat_`i'[3,3]+mat_`i'[4,3]+mat_`i'[5,3]
sca female_4_`i'_marr=mat_`i'[1,4]+mat_`i'[2,4]+mat_`i'[3,4]+mat_`i'[4,4]+mat_`i'[5,4]
sca female_5_`i'_marr=mat_`i'[1,5]+mat_`i'[2,5]+mat_`i'[3,5]+mat_`i'[4,5]+mat_`i'[5,5]

qui tab ambition if koen=="1" & relationship==0 & aar==`i', matcell(am_single_m_`i')
sca male_1_`i'_sing=am_single_m_`i'[1,1]
sca male_2_`i'_sing=am_single_m_`i'[2,1]
sca male_3_`i'_sing=am_single_m_`i'[3,1]
sca male_4_`i'_sing=am_single_m_`i'[4,1]
sca male_5_`i'_sing=am_single_m_`i'[5,1]


qui tab ambition if koen=="2" & relationship==0 & aar==`i', matcell(am_single_f_`i')
sca female_1_`i'_sing=am_single_f_`i'[1,1]
sca female_2_`i'_sing=am_single_f_`i'[2,1]
sca female_3_`i'_sing=am_single_f_`i'[3,1]
sca female_4_`i'_sing=am_single_f_`i'[4,1]
sca female_5_`i'_sing=am_single_f_`i'[5,1]

sca am_pi_1_`i'=mat_`i'[1,1]/((male_1_`i'_sing*female_1_`i'_sing)^(1/2))
sca am_pi_2_`i'=mat_`i'[2,2]/((male_2_`i'_sing*female_2_`i'_sing)^(1/2))
sca am_pi_3_`i'=mat_`i'[3,3]/((male_3_`i'_sing*female_3_`i'_sing)^(1/2))
sca am_pi_4_`i'=mat_`i'[4,4]/((male_4_`i'_sing*female_4_`i'_sing)^(1/2))
sca am_pi_5_`i'=mat_`i'[5,5]/((male_5_`i'_sing*female_5_`i'_sing)^(1/2))


}


frame copy default temp
frame change temp
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen diag=.
foreach i in 1980 1981 2018{
replace diag=diag_`i' if aar==`i' 
}

forvalues m=1(1)5{
	forvalues f=1(1)5{
gen s_`m'_`f'=.
foreach i in 1980 1981 2018{
replace s_`m'_`f'=s_`m'_`f'_`i' if aar==`i' 
}
}
}

gen lambda=.
replace lambda=lambda_1981 if aar==1980 
foreach i in 1981 2018{
replace lambda=lambda_`i' if aar==`i' 
}

forvalues m=1(1)5{
	forvalues f=1(1)5{
gen weight_`m'_`f'=.
foreach i in 1980 1981 2018{
replace weight_`m'_`f'=weight_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)5{
	forvalues f=1(1)5{
gen S_`m'_`f'=.
foreach i in 1980 1981 2018{
replace S_`m'_`f'=S_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)5{
gen male_`m'=.
foreach i in 1980 1981 2018{
replace male_`m'=male_`m'_`i' if aar==`i' 
}
}

forvalues f=1(1)5{
gen female_`f'=.
foreach i in 1980 1981 2018{
replace female_`f'=female_`f'_`i' if aar==`i' 
}
}

forvalues m=1(1)5{
gen male_`m'_marr=.
foreach i in 1980 1981 2018{
replace male_`m'_marr=male_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)5{
gen male_`m'_sing=.
foreach i in 1980 1981 2018{
replace male_`m'_sing=male_`m'_`i'_sing if aar==`i' 
}
}

forvalues m=1(1)5{
gen female_`m'_marr=.
foreach i in 1980 1981 2018{
replace female_`m'_marr=female_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)5{
gen female_`m'_sing=.
foreach i in 1980 1981 2018{
replace female_`m'_sing=female_`m'_`i'_sing if aar==`i' 
}
}



forvalues m=1(1)5{
gen am_pi_`m'=.
foreach i in 1980 1981 2018{
replace am_pi_`m'=am_pi_`m'_`i' if aar==`i' 
}
}




save "Results\tab_B3\Scenario_6\Sorting\results_ambition.dta", replace

